package com.horay.nettydemo.nio.buffer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.unit.DataSize;

import java.nio.IntBuffer;

/**
 * Buffer案例
 *
 * @author heyingcheng
 * @date 2019/11/23 16:52
 */
@Slf4j
public class BasicBuffer {

    public static void main(String[] args) {
        IntBuffer intBuffer = IntBuffer.allocate(Math.toIntExact(DataSize.ofBytes(10L).toBytes()));

        for (int i = 0, length = intBuffer.capacity(); i < length; i++) {
            intBuffer.put(2 * i);
        }

        // 切换写
        intBuffer.flip();

        while (intBuffer.hasRemaining()) {
            int value = intBuffer.get();
            log.info("value:[{}]", value);
        }
    }

}
